package com.pengzy.comment.web.webFluxUtils;

import com.pengzy.comment.web.result.JsonResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author xiaozhi 2023/7/18
 */

public class ResponseResult {

    public  static Mono<Void> writeWith(ServerWebExchange exchange, JsonResult responseMap){
        ServerHttpResponse response = exchange.getResponse();
        //String body = JSONObject.toJSONString(responseMap);
        DataBuffer buffer = response.bufferFactory().wrap(com.alibaba.fastjson.JSONObject.toJSONBytes(responseMap));
        return response.writeWith(Mono.just(buffer));
    }

    public  static Mono<Object> writeWith2(ServerWebExchange exchange, JsonResult responseMap){
        ServerHttpResponse response = exchange.getResponse();
        //String body = JSONObject.toJSONString(responseMap);
        DataBuffer buffer = response.bufferFactory().wrap(com.alibaba.fastjson.JSONObject.toJSONBytes(responseMap));
        response.writeWith(Mono.just(buffer));
        return Mono.just(null);
    }
}
